package net.daum.codegen;

import java.awt.Desktop;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.daum.codegen.dao.SchemaDao;
import net.daum.codegen.domain.Part;
import net.daum.codegen.domain.Table;

import org.apache.commons.io.FileUtils;

import com.google.common.collect.Lists;

public class GenDomain {
	public static void main(String[] args) throws Exception {
		SchemaDao schemaDao = DatabaseUtil.getDao();
		
		Table table = new Table();
		table.setSchema("test");
		table.setName("test");
		
		table.setColumnList(schemaDao.selectColumnList(table));
		table.setPackageName("net.daum.event.domain");
		
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		Map<String, Object> partMap = new LinkedHashMap<String, Object>();
		List<Part> partList = Lists.newArrayList(CodeGenUtil.getPartmap().values());
				
		map.put("codeGenUtil", CodeGenUtil.class);
		
		for (Part part : partList) {			
			Map<String, Object> domainMap = new LinkedHashMap<String, Object>();
			domainMap.put("table", table);
			domainMap.put("genProject", GenProject.class);
			String content = VelocityProcess.getResult("velocity/" + part.getName() + ".vm", domainMap);
			title(part.getName());
			System.out.println(content);
			partMap.put(part.getName(), content);			
		}
		
		map.put("partmap", partMap);
		map.put("table", table);
		
		File resultFile = new File("result.html");
		FileUtils.writeStringToFile(new File("result.html"), VelocityProcess.getResult("velocity/result.vm", map));
		Desktop.getDesktop().open(resultFile);		
	}
	
	public static void title(String title) {
		System.out.println("=====================================");
		System.out.println("     " + title);
		System.out.println("=====================================");
	}
}
